package cn.raven.user.controller;

import cn.raven.basic.annotation.PreAuthorize;
import cn.raven.basic.dto.EmailRegisterDto;
import cn.raven.basic.dto.VerifyRegisterDto;
import cn.raven.basic.exception.BusinessException;
import cn.raven.user.service.IUserService;
import cn.raven.user.domain.User;
import cn.raven.user.query.UserQuery;
import cn.raven.basic.utils.AjaxResult;
import cn.raven.basic.utils.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    public IUserService userService;

    /** 
    * @Title: loginOut
    * @Description: 登出
    * @Author: king
    * @Version: 1.0
    * @Date:  2022/7/10 15:16
    * @Parameters: [request]
    * @Return cn.raven.basic.utils.AjaxResult
      */
    @GetMapping("/loginOut")
    @PreAuthorize(name = "前台用户登出",sn = "user:loginOut")
    public AjaxResult loginOut(HttpServletRequest request){
        try {
            userService.loginOut(request);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙");
        }

    }
    /**
    * @Title: register
    * @Description: 手机注册
    * @Author: king
    * @Version: 1.0
    * @Date:  2022/7/7 18:03
    * @Parameters: [verifyRegisterDto]
    * @Return cn.raven.basic.utils.AjaxResult
      */
    @PostMapping("/register/phone")
    public AjaxResult register(@RequestBody VerifyRegisterDto verifyRegisterDto){
        try {
            userService.register(verifyRegisterDto);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage()).setSuccess(false);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统错误,请骚后重试").setSuccess(false);
        }

    }
    
    /** 
    * @Title: email
    * @Description: 邮箱注册
    * @Author: king
    * @Version: 1.0
    * @Date:  2022/7/7 18:25  
    * @Parameters: [emailRegisterDto]
    * @Return cn.raven.basic.utils.AjaxResult
      */
    @PostMapping("/register/email")
    public AjaxResult email(@RequestBody EmailRegisterDto emailRegisterDto){
        try {
            userService.email(emailRegisterDto);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage()).setSuccess(false);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统错误,请骚后重试").setSuccess(false);
        }

    }




    /**
     * 保存和修改公用的
     * @param user  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    @PreAuthorize(name = "保存前台用户",sn = "user:save")
    public AjaxResult save(@RequestBody User user){
        try {
            if( user.getId()!=null)
                userService.update(user);
            else
                userService.add(user);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    @PreAuthorize(name = "删除前台用户",sn = "user:delete")
    public AjaxResult del(@PathVariable("id") Long id){
        try {
            userService.del(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除对象失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    @PreAuthorize(name = "id查询前台用户",sn = "user:id")
    public User findById(@PathVariable("id")Long id)
    {
        return userService.findOne(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<User> findAll(){

        return userService.findAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    @PreAuthorize(name = "前台用户列表",sn = "user:list")
    public PageList<User> queryData(@RequestBody UserQuery query)
    {
        return userService.queryData(query);
    }
}
